package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.TermInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.TermInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.TermInfo;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.TermInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntTermInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntTermInfoQueryResponse;
import cc.rengu.igas.share.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 商户终端信息查询
 * Created by wangcheng on 2021/10/22 16:20
 */
public class MchntTermInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntTermInfoQueryRequest mchntTermInfoQueryRequest = new MchntTermInfoQueryRequest();
        ConvertUtil.convertOutput(mchntTermInfoQueryRequest);
        return mchntTermInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntTermInfoQueryRequest mchntTermInfoQueryRequest = (MchntTermInfoQueryRequest) request;
        String instId = mchntTermInfoQueryRequest.getHeader().getInstId();
        String mchntNo = mchntTermInfoQueryRequest.getMchntNo();
        String termNo = mchntTermInfoQueryRequest.getTermNo();

        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(termNo)) {
            rglog.error("法人标识:<{}>,商户号:<{}>或者终端号:<{}>某个值为空！", instId, mchntNo, termNo);
            throw new BizException(RespCodeEnum.INVALID_REQEST_PARAM.getRespCode(), RespCodeEnum.INVALID_REQEST_PARAM.getRespDesc());
        }

        MchntTermInfoQueryResponse mchntTermInfoQueryResponse = new MchntTermInfoQueryResponse();
        mchntTermInfoQueryResponse.setHeader(mchntTermInfoQueryRequest.getHeader());
        TermInfoMapper termInfoMapper = new TermInfoMapperImpl();
        TermInfo termInfo = termInfoMapper.selectBompTermInfoByPrimaryKey(instId, mchntNo, termNo);
        if (null != termInfo) {
            TermInfoBean termInfoBean = new TermInfoBean();
            termInfoBean.setInstId(termInfo.getInstId());
            termInfoBean.setMchntNo(termInfo.getMchntNo());
            termInfoBean.setTermNo(termInfo.getTermNo());
            termInfoBean.setTermType(termInfo.getTermType());
            termInfoBean.setAcqInstId(termInfo.getPosInstCd());
            termInfoBean.setTermAreaCode(termInfo.getTermAddrArea());
            termInfoBean.setLongitude(termInfo.getLongitude());
            termInfoBean.setLatitude(termInfo.getLatitude());
            termInfoBean.setEffectiveRadius(termInfo.getEffectiveRadius());
            termInfoBean.setTermSn(termInfo.getSn());
            termInfoBean.setIcParamDownFlag(termInfo.getIcDownSign());
            termInfoBean.setIcKeyDownFlag(termInfo.getKeyDownSign());
            termInfoBean.setTermSignStatus(termInfo.getTermSignStatus());
            termInfoBean.setAutoSignOutFlag(termInfo.getAutoSignoutFlag());
            termInfoBean.setBatchNo(termInfo.getBathcSettleNum());
            termInfoBean.setTermStatus(termInfo.getTermStatus());
            termInfoBean.setContactName(termInfo.getContactName());
            termInfoBean.setContactPhone(termInfo.getContactPhone());
            mchntTermInfoQueryResponse.setTermInfo(termInfoBean);
        }
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntTermInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntTermInfoQueryResponse mchntTermInfoQueryResponse = (MchntTermInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntTermInfoQueryResponse);
    }
}
